

import 'package:package_model/event.dart';
import 'package:package_model/url_data.dart';
import 'package:user_data_model/data/token_data.dart';
import 'package:user_data_model/data/user_data.dart';
import 'package:user_data_model/model/login_model.dart';
import 'package:user_data_model/model/user_model.dart';
import 'package:user_data_model/token_util/auth_by_form.dart';
import 'package:user_data_model/user_data_model.dart';
import 'package:user_data_model/user_register_item.dart';
import 'package:fresh/fresh.dart' as fresh;
import 'package:fresh_dio/fresh_dio.dart';
import 'package:event_bus/event_bus.dart';

/**
 *
 * @ProjectName:    login_model
 * @ClassName:      login_bloc
 * @Description:    dart类作用描述
 * @Author:         孙浩
 * @QQ:             243280864
 * @CreateDate:     2021/1/8 7:33 PM
 */
class LoginBloc {
  String tel, passWord;

  LoginModel _model = LoginModel(UserRegister.instance.baseDio);
  UserModel _userModel = UserModel(UserRegister.instance.baseDio);
  Future loginByPassWord() async {
    TokenData data =await _model.loginByPassowrd(
        LoginData(tel:tel ,pwd: passWord,deviceToken:"AgF6Zj1zyVxGpOz2Zo52z-WzqsWPZzm_mISxtJ-MX8k" ));
    InMemoryTokenStorage<OAuth2Token> _memoryTokenStorage = InMemoryTokenStorage();
    await _memoryTokenStorage.write(OAuth2Token(accessToken: data.data.accessToken,refreshToken: data.data.refreshToken));
    print("这里不太对啊${_memoryTokenStorage}");
    UserRegister.instance.baseDio.interceptors.add(AurhFreshByForm.oAuth2(
        tokenStorage: _memoryTokenStorage,
        refreshToken: (token, client) async {
          var res = await Dio().post<TokenData>(StaticUrl.BaseUrl+"appapi/account/refreshToken",data: {
            "refreshToken":token.refreshToken
          });
          TokenData _data = res.data;
          return OAuth2Token(accessToken: _data.data.accessToken,refreshToken: _data.data.refreshToken);
        },
        tokenHeader:(OAuth2Token t){
          return {"Authorization":"${t.accessToken}"};
        }
    ),);
    print(UserRegister.instance.baseDio.interceptors);
    UserData userData = await _userModel.getUserInfoFromNet();
    UserDataPlugin.saveUserData(userData);
    EventUtil.loginEvent.fire(LoginType.login);
    return;
  }

  void saveName(String name) {
    this.tel = name;
  }

  void savePwd(String pwd) {
    this.passWord = pwd;
  }
}